
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[EBAS_SP_HENT_DATO_NOTAT]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[EBAS_SP_HENT_DATO_NOTAT]
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE [DBO].[EBAS_SP_HENT_DATO_NOTAT] 
	@session varchar(255),
	@studGuid varchar(255)
WITH ENCRYPTION
AS
	DECLARE 
		@rettighed int,
		@antal int
	
	SET NOCOUNT ON

	CREATE TABLE #temp ( rett int )
	INSERT INTO #temp (rett)
	EXEC EBAS.DBO.EBAS_SP_SIKKERHED_VERIFICER_SESSION @session	

	SET @rettighed = (SELECT IsNull(max(rett),0) FROM #temp)

	IF 0 < @rettighed 
	BEGIN
		SET @antal = (	SELECT COUNT(*) 
				FROM EBAS_DATO_NOTAT 
				WHERE STUDERENDE_GUID = @studGuid)
		
		SELECT GUID, DATO, EMNE, FIL_NAVN, NOTAT_BINARY, STUDERENDE_GUID, @antal AS ANTAL
		FROM EBAS_DATO_NOTAT
		WHERE STUDERENDE_GUID = @studGuid
		ORDER BY DATO DESC
	END
	ELSE
		SELECT GUID, DATO, EMNE, FIL_NAVN, NOTAT_BINARY, STUDERENDE_GUID, 0 AS ANTAL
		FROM EBAS_DATO_NOTAT
		WHERE GUID IS NULL
		ORDER BY DATO DESC	
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO
